package com.krahets88.hashtable;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;

/**
 * https://leetcode.cn/problems/valid-anagram/
 */
public class Question242 {
    @Test
    public void test1(){
        Solution1 solution1 = new Solution1();
        System.out.println(solution1.isAnagram("nagaram","anagram"));
    }
}
class Solution1 {
    /**
     *
     * @param s
     * @param t
     * @return
     */

    public boolean isAnagram(String s, String t) {
        char[] charArray = s.toCharArray();
        char[] charArray1 = t.toCharArray();
        Arrays.sort(charArray1);
        Arrays.sort(charArray);
        String str1= Arrays.toString(charArray);
        String str2= Arrays.toString(charArray1);
        return str1.equals(str2);
    }
}

class Solution2 {
    /**
     *
     * @param s
     * @param t
     * @return
     */

    public boolean isAnagram(String s, String t) {
        HashMap<Character,Integer> dict = new HashMap<>();
        for(int i=0;i<s.length();i++){
            if(dict.containsKey(s.charAt(i))){
                dict.put(s.charAt(i),dict.get(s.charAt(i))+1);
            }else {
                dict.put(s.charAt(i),1);
            }
        }
        for(int i=0;i<t.length();i++){
            if(!dict.containsKey(t.charAt(i))){
                return false;
            }else {
                dict.put(t.charAt(i),dict.get(t.charAt(i))-1);
            }
        }
        for (int i:dict.values()){
            if(i != 0){
                return false;
            }
        }
        return true;
    }
}
